/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.client.config.TargetProjectConfig;
import com.tmall.testgen.plugin.Activator;
import com.tmall.testgen.plugin.ConsoleFactory;
import com.tmall.testgen.plugin.views.vo.SpringTree;
import com.tmall.testgen.plugin.views.vo.TreeParent;
import com.tmall.testgen.plugin.wizards.runinspring.listener.AddTestBtnSelectListener;
import com.tmall.testgen.plugin.wizards.runinspring.listener.ComputeBtnSelectListener;
import com.tmall.testgen.plugin.wizards.runinspring.listener.StartSpringBtnSelectListener;

public class ComCreator {
	public static final String UPDATE_CONFIG_ID = "updateConfigId";
	public static final String START_CONFIG_ID = "startConfigId";

	/**
	 * 计算按钮
	 */
	public static final String COMPUTE_BTN_ID = "computeBtnId";
	/**
	 * 添加至单元测试按钮
	 */
	public static final String ADD_TO_TEST_BTN_ID = "addToTestBtnId";
	public static final String HSF_CONFIG_BTN_ID="hsfConfigBtn";
	public static final String HSF_CONFIG_SELECT_BTN_ID="hsfConfigSelectBtn";
	public static final String HSF_CONFIG_TEXT_ID="hsfConfigtText";
	public static final String MOCK_TEXT_ID="mockText";
	
	
	
	
	private static final Map<String, Control > btnMap = new HashMap<String, Control >();

    
    /**
     * 生成类配置
     * 
     * @param comConfig
     */
    public static void createMockComClass(Composite parent,final  RunInSpringWizadPage page) {
        Composite com = new Composite(parent, SWT.NONE | SWT.BORDER);
        GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
        com.setLayoutData(comGd);

        GridLayout layout = new GridLayout();
        layout.numColumns = 3;
        com.setLayout(layout);

        Label mocklbl = new Label(com, SWT.NONE);
        GridData mocklblGd = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
        mocklbl.setLayoutData(mocklblGd);
        mocklbl.setText("mock表达式");

        final Text mockText = new Text(com, SWT.NONE | SWT.BORDER);
        GridData mockGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
        mockText.setLayoutData(mockGd);
        
        btnMap.put(MOCK_TEXT_ID, mockText);
        
        String mockDes=Activator.getDefault().getConfig(page.getTargetProjectPath()+"mockDes");
        if( mockDes!=null&&mockDes.length()>0){
            mockText.setText(mockDes);
            ClientDataCenter.getInstance().setMockDes(mockDes);
        }
        mockText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                ClientDataCenter.getInstance().setMockDes(mockText.getText());
                Activator.getDefault().setConfig(page.getTargetProjectPath()+"mockDes", mockText.getText());
            }
        });
        
        
    }
	/**
	 * 生成类配置
	 * 
	 * @param comConfig
	 */
	public static void createComClass(Composite parent,RunInSpringWizadPage page) {
		Composite com = new Composite(parent, SWT.NONE | SWT.BORDER);
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		com.setLayoutData(comGd);

		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		com.setLayout(layout);

		Label lbl = new Label(com, SWT.NONE);
		GridData lblGd = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
		lbl.setLayoutData(lblGd);
		lbl.setText("方法名");

		Text methodNameText = new Text(com, SWT.NONE | SWT.BORDER);
		GridData textGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		methodNameText.setLayoutData(textGd);
		page.setMethodNameText(methodNameText);
		// TODO 初始java类
		try {
			methodNameText.setText(page.getMethodName());
			methodNameText.setEnabled(false);
		} catch (Exception e) {
			ConsoleFactory.putError(e);
		}

        
	}

	/**
	 * 生成测试代码
	 * 
	 * @param comConfig
	 */
	public static void createComGenCode(Composite parent,
			TreeParent genCodeTreeRoot, RunInSpringWizadPage page) {
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3);
		comGd.minimumHeight = 100;
		Composite com = new Composite(parent, SWT.NONE | SWT.BORDER);
		com.setLayoutData(comGd);

		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		com.setLayout(layout);
		TreeCreator.createGenCodeTree(com, genCodeTreeRoot, page);
	}

	public static void createComMethod(Composite parent,
			RunInSpringWizadPage page) {
		Composite com = new Composite(parent, SWT.NONE | SWT.BORDER);
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 5);
		comGd.minimumHeight = 200;
		com.setLayoutData(comGd);

		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		com.setLayout(layout);
		// GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		// //方法列表
		// Composite comList = new Composite(com,SWT.NONE|SWT.BORDER);
		// comList.setLayoutData(comGd);
		// createMethodList(comList);
		// 参数区
		Composite comPara = new Composite(com, SWT.NONE | SWT.BORDER);
		comPara.setLayoutData(comGd);
		createComParas(comPara, page);
		// 计算结果
		Composite comResult = new Composite(com, SWT.NONE | SWT.BORDER);
		comResult.setLayoutData(comGd);
		createComResult(comResult, page.getResultRoot(), page);
	}

	/**
	 * 生成HSF文件
	 * 
	 * @param com
	 */
	public static void createComHSF(final Composite parent,
			final RunInSpringWizadPage page) {

		Composite com = new Composite(parent, SWT.NONE | SWT.BORDER);
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		com.setLayoutData(comGd);

		GridLayout layout = new GridLayout();
		layout.numColumns = 5;
		layout.makeColumnsEqualWidth = false;
		com.setLayout(layout);
		// 设置HSF
		Label hsfLbl = new Label(com, SWT.NONE);
		hsfLbl.setText("使用HSF");
		GridData hsfLblGd = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		hsfLbl.setLayoutData(hsfLblGd);

		final Button hsfBtn = new Button(com, SWT.NONE | SWT.CHECK);
		// hsfBtn.setText("使用HSF");
		GridData hsfBtnGd = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
		hsfBtn.setLayoutData(hsfBtnGd);

		final Label hsfPathLbl = new Label(com, SWT.NONE);
		hsfPathLbl.setText("配置路径:");
		GridData hsfPathLblGd = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		hsfPathLbl.setLayoutData(hsfPathLblGd);

		final Text hsfText = new Text(com, SWT.NONE | SWT.BORDER);
		GridData hsftextGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		hsfText.setLayoutData(hsftextGd);

		final Button hsfFileBtn = new Button(com, SWT.NONE);
		hsfFileBtn.setText("选择路径");
		GridData hsfFileBtnGd = new GridData(SWT.RIGHT, SWT.FILL, false, false,1, 1);
		hsfFileBtn.setLayoutData(hsfFileBtnGd);
		
		Boolean isUseHsf=false;
		try{
			isUseHsf=Boolean.valueOf(Activator.getDefault().getConfig(page.getTargetProjectPath() + "isUseHsf"));
			String hsfConfig=Activator.getDefault().getConfig(page.getTargetProjectPath() + "hsfConfig");
			hsfText.setText(hsfConfig==null?"":hsfConfig);
			
			ClientDataCenter.getInstance().setIsUseHsf(isUseHsf);
			
			ClientDataCenter.getInstance().setHsfConfig(hsfConfig);
		}catch(Exception e){
			
		}
		hsfPathLbl.setEnabled(isUseHsf);
		hsfText.setEnabled(false);
		hsfFileBtn.setEnabled(isUseHsf);
		hsfBtn.setSelection(isUseHsf);
		
		hsfBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				boolean bool = hsfBtn.getSelection();
				hsfPathLbl.setEnabled(bool);
				hsfText.setEnabled(false);
				hsfFileBtn.setEnabled(bool);
				ClientDataCenter.getInstance().setIsUseHsf(bool);
				Activator.getDefault().setConfig(page.getTargetProjectPath() + "isUseHsf",bool+"");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
		});
		hsfFileBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(parent.getShell());
				dialog.setText("选择HSF文件路径：");
				String filePath = dialog.open();
				if (filePath != null) {
					hsfText.setText(filePath);
					ClientDataCenter.getInstance().setHsfConfig(filePath);
					Activator.getDefault().setConfig(page.getTargetProjectPath() + "hsfConfig",filePath);
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
		});
		btnMap.put(HSF_CONFIG_BTN_ID, hsfBtn);
		btnMap.put(HSF_CONFIG_SELECT_BTN_ID, hsfFileBtn);
		btnMap.put(HSF_CONFIG_TEXT_ID, hsfPathLbl);
		
		
	}

	/**
	 * 生成配置文件
	 * 
	 * @param com
	 */
	public static void createComConfig(Composite parent,
			final RunInSpringWizadPage page) {

		Composite com = new Composite(parent, SWT.NONE | SWT.BORDER);
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		com.setLayoutData(comGd);

		GridLayout layout = new GridLayout();
		layout.numColumns = 4;
		layout.makeColumnsEqualWidth = false;
		com.setLayout(layout);

		GridData textGd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		final Text springFileText = new Text(com, SWT.NONE | SWT.BORDER);
		springFileText.setLayoutData(textGd);
		// TODO 初始spring配置文件
		springFileText.setText("");
		springFileText.setEnabled(false);
		if (Activator.getDefault().getConfig(page.getTargetProjectPath() + "filePath") != null) {
			springFileText.setText(Activator.getDefault().getConfig(page.getTargetProjectPath() + "filePath"));
			String projectName=Activator.getDefault().getConfig(page.getTargetProjectPath() + "projectName");
			TargetProjectConfig.getInstance().setTargetProjectPath(projectName,Activator.getDefault().getConfig(page.getTargetProjectPath() + "projectPath"));
			TargetProjectConfig.getInstance().setTargetProjectConfigPath(springFileText.getText());
		}
		page.setSpringFileText(springFileText);

		GridData btnGd = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1);
		Button updateFileBtn = new Button(com, SWT.NONE | SWT.PUSH);
		updateFileBtn.setText("选择配置文件");
		updateFileBtn.setLayoutData(btnGd);
		updateFileBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				FileSelectionWizard wizard = new FileSelectionWizard(true);
				// WizardDialog dialog = new WizardDialog(getShell(), wizard);
				WizardDialog dialog = new WizardDialog(null, wizard);

				dialog.setPageSize(600, 400);
				dialog.create();
				int returnv = dialog.open();
				if (returnv == 0) {
					springFileText.setText(wizard.getFilePath());
					Activator.getDefault().setConfig(page.getTargetProjectPath() + "filePath",wizard.getFilePath());
					Activator.getDefault().setConfig(page.getTargetProjectPath() + "projectPath",wizard.getProjectPath());
					Activator.getDefault().setConfig(page.getTargetProjectPath() + "projectName",wizard.getProject().getName());
					TargetProjectConfig.getInstance().setTargetProjectPath(wizard.getProject().getName(),wizard.getProjectPath());
					TargetProjectConfig.getInstance().setTargetProjectConfigPath(wizard.getFilePath());
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});

		btnMap.put(UPDATE_CONFIG_ID, updateFileBtn);
		Button startSpringBtn = new Button(com, SWT.NONE | SWT.PUSH);
		startSpringBtn.setText("启动Spring");
		startSpringBtn.setLayoutData(btnGd);
		startSpringBtn.addSelectionListener(new StartSpringBtnSelectListener(springFileText, btnMap, page));
		btnMap.put(START_CONFIG_ID, startSpringBtn);
		// Button stopSpringBtn = new Button(com, SWT.NONE | SWT.PUSH);
		// stopSpringBtn.setText("停止Spring");
		// stopSpringBtn.setLayoutData(btnGd);
		// stopSpringBtn.setEnabled(false);
		// stopSpringBtn.addSelectionListener(new
		// StopSpringBtnSelectListener(btnMap));
		// btnMap.put(STOP_CONFIG_ID, stopSpringBtn);

	}

	/**
	 * 生成方法区
	 * 
	 * @param comConfig
	 */
	private static void createComResult(Composite com, SpringTree resultRoot,
			RunInSpringWizadPage page) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		com.setLayout(layout);
		GridData labelGd = new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1);
		Label paraLabel = new Label(com, SWT.NONE);
		paraLabel.setText("计算结果:");
		paraLabel.setLayoutData(labelGd);

		TreeCreator.createResultTree(com, resultRoot, page);

		// GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		// Composite parasCom = new Composite(com, SWT.NONE | SWT.BORDER);
		// parasCom.setLayoutData(comGd);
	}

	/**
	 * 生成参数区
	 * 
	 * @param comConfig
	 */
	private static void createComParas(Composite com, RunInSpringWizadPage page) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		com.setLayout(layout);

		GridData labelGd = new GridData(SWT.LEFT, SWT.TOP, false, false, 2, 1);
		Label paraLabel = new Label(com, SWT.NONE);
		paraLabel.setText("入参:");
		paraLabel.setLayoutData(labelGd);

		TreeCreator.createParasTree(com, page.getParasRoot(), page);

		GridData btnGd = new GridData(SWT.FILL, SWT.END, true, false, 1, 1);
		Button excuteBtn = new Button(com, SWT.NONE | SWT.PUSH);
		excuteBtn.setText("计算");
		excuteBtn.setEnabled(false);
		excuteBtn.setLayoutData(btnGd);
		btnMap.put(COMPUTE_BTN_ID, excuteBtn);

		excuteBtn.addSelectionListener(new ComputeBtnSelectListener(page));
		Button addTestBtn = new Button(com, SWT.NONE | SWT.PUSH);
		addTestBtn.setText("添加至单元测试");
		addTestBtn.setLayoutData(btnGd);
		addTestBtn.setEnabled(false);
		addTestBtn.addSelectionListener(new AddTestBtnSelectListener(page));
		btnMap.put(ADD_TO_TEST_BTN_ID, addTestBtn);
	}

}
